﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using unifiqAnthony.Class;
using System.Collections.ObjectModel;
using unifiqAnthony.UnifiqSvc;
using System.Text;

namespace unifiqAnthony
{
    public partial class ListingList : PhoneApplicationPage
    {

        ByteToImageConverter c = new ByteToImageConverter();
        public ListingList()
        {
            InitializeComponent();
            var obj = App.Current as App;

            progressOverlay.Show();
            UnifiqSvc.ServiceSoapClient db  = new UnifiqSvc.ServiceSoapClient();
            db.getListingCompleted +=new EventHandler<UnifiqSvc.getListingCompletedEventArgs>(displayListing);
            db.getListingAsync(obj.uid);

        }

        

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string temp;
            if (NavigationContext.QueryString.TryGetValue("back", out temp))
            {
                NavigationService.RemoveBackEntry();
            }
        }

        void displayListing(object sender, UnifiqSvc.getListingCompletedEventArgs e)
        {
            progressOverlay.Hide();

            Listing[] listingList = e.Result;
            //List<Listing> list = l;

            ObservableCollection<SampleData> dataSource = new
            ObservableCollection<SampleData>();
            foreach (Listing l in listingList)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("Blk " + l.blkNo + " " + l.stName + " St. " + l.stNo + "\n");
                builder.Append("$" + l.fee + " / Tenant \n");
                builder.Append(l.noOfTenants + " Tenant(s)");

                String v = "Collapsed";
                if (l.available == 'n')
                {
                    v = "Visible";
                }

                dataSource.Add(new SampleData()
                {
                    bmp = c.ConvertByteArrayToBitMapImage(l.img1),
                    text = builder.ToString(),
                    visibility = v,
                    listing = l
                });

                
            }
            this.list.ItemsSource = dataSource; 
        }

        private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Get the data object that represents the current selected item
            SampleData data = (sender as ListBox).SelectedItem as SampleData;
            Listing l = data.listing;

            // Set selected Listing to obj.listing
            var obj = App.Current as App;
            obj.listing = l;

            List<byte[]> byteList = new List<byte[]>();
            byteList.Add(null); byteList.Add(null); byteList.Add(null); // Create 3 slots

            if(l.img1!=null)
                byteList[0]=l.img1;
            if (l.img2 != null)
                byteList[1] = l.img2;
            if (l.img3 != null)
                byteList[2] = l.img3;

            obj.byteList = byteList;
            NavigationService.Navigate(new Uri("/ListingView.xaml", UriKind.Relative));

            //Get the selected ListBoxItem container instance    
            //ListBoxItem selectedItem = this.list.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;
        }
    }
}